<template>
  <el-form v-loading="userloading" label-position="right" label-width="100px">
    <el-form-item label="用户名" style="float:left;width:300px">
      {{ user.username }}
    </el-form-item>
    <el-form-item label="姓名" style="float:left;width:300px">
      {{ user.realName }}
    </el-form-item>
    <el-form-item label="用户角色" style="float:left;width:300px">
      {{ user.roleName }}
    </el-form-item>
    <el-form-item label="单位" style="clear:both;float:left;width:300px">
      {{ user.unitName }}
    </el-form-item>
    <!-- <el-form-item label="部门" style="float:left;width:300px">
      {{ user.deptName }}
    </el-form-item> -->
    <el-form-item label="职位" style="float:left;width:300px">
      {{ user.post }}
    </el-form-item>
    <!-- <el-form-item label="房间号" style="clear:both">
      <el-input v-model.trim="user.roomNo" style="width:300px" />
    </el-form-item> -->
    <el-form-item label="电话" style="clear:both">
      <el-input v-model.trim="user.phone" style="width:300px" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" :loading="loading" @click="submit">保存</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
export default {
  data() {
    return {
      userloading: false,
      loading: false,
      myHeaders: {},
      user: {
        id: null,
        username: '',
        deptName: '',
        roleName: '',
        phone: '',
        email: '',
        remark: ''
      },
      imageUrl: null
    }
  },
  created() {
    this.getUser()
  },
  mounted() {
    this.myHeaders = {
      Authorization: 'Bearer ' + localStorage.getItem('token')
    }
  },
  methods: {
    getsrc(id) {
      this.$http
        .get('/api/gen/file/download?id=' + parseInt(id))
        .then(response => {
          if (response.data.success === true) {
            return response.data.result
          } else {
            console.log('获取失败:' + response.data.message)
            this.$message({
              message: '获取失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(function(error) {
          console.log(error)
        })
    },
    getUser() {
      this.user = JSON.parse(localStorage.getItem('user'))
      // this.userloading = true
      // this.$http
      //   .get('/api/sysUser/getPersonalInfo')
      //   .then(response => {
      //     this.userloading = false
      //     if (response.data.success === true) {
      //       this.user = response.data.result
      //       this.imageUrl = response.data.result.headImg
      //     } else {
      //       console.log('获取失败:' + response.data.message)
      //       this.$message({
      //         message: '获取失败:' + response.data.message,
      //         type: 'error'
      //       })
      //     }
      //   })
      //   .catch(function(error) {
      //     console.log(error)
      //     this.userloading = false
      //   })
      // this.user = {
      //   name: this.name,
      //   role: this.roles.join(' | '),
      //   email: 'admin@test.com',
      //   avatar: this.avatar
      // }
    },
    handleAvatarSuccess(res, file) {
      // this.imageUrl = URL.createObjectURL(file.raw)
      console.log('test')
      if (res.success) {
        this.$message({
          message: '上传成功！',
          type: 'success'
        })
        this.imageUrl = res.result[0].id
        // this.fileList = []
        // this.files = []
      } else {
        this.$message({
          message: '上传失败:' + res.message,
          type: 'error'
        })
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isPNG = file.type === 'image/png'
      const isGIF = file.type === 'image/gif'
      const isLt20M = file.size / 1024 / 1024 < 20

      if (!isJPG && !isPNG && !isGIF) {
        this.$message.error('上传图片只能是 JPG 或 PNG 格式!')
        return false
      }
      if (!isLt20M) {
        this.$message.error('上传图片大小不能超过 20MB!')
        return false
      }
    },
    submit() {
      const para = {
        id: this.user.id,
        phone: this.user.phone,
        roomNo: this.user.roomNo
      }
      this.$http
        .post('/api/sysUser/savePersonalInfo', para)
        .then(response => {
          this.loading = false
          if (response.data.success === true) {
            this.$message({
              message: '保存成功',
              type: 'success'
            })
            localStorage.setItem('user', JSON.stringify(this.user))
            // this.$emit('changeImage')
          } else {
            console.log('保存失败:' + response.data.message)
            this.$message({
              message: '保存失败:' + response.data.message,
              type: 'error'
            })
          }
        })
        .catch(error => {
          console.log(error)
          this.loading = false
        })
      // this.$message({
      //   message: 'User information has been updated successfully',
      //   type: 'success',
      //   duration: 5 * 1000
      // })
    }
  }
}
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
